export function dateFormat(fmt, date) {
  let ret = ""
  date = new Date(date)
  const opt = {
    "Y+": date.getFullYear().toString(), // 年
    "m+": (date.getMonth() + 1).toString(), // 月
    "d+": date.getDate().toString(), // 日
    "H+": date.getHours().toString(), // 时
    "M+": date.getMinutes().toString(), // 分
    "S+": date.getSeconds().toString() // 秒
    // 有其他格式化字符需求可以继续添加，必须转化成字符串
  }
  for (let k in opt) {
    ret = new RegExp("(" + k + ")").exec(fmt)
    if (ret) {
      fmt = fmt.replace(ret[1], ret[1].length == 1 ? opt[k] : opt[k].padStart(ret[1].length, "0"))
    }
  }
  return fmt
}

// 计算时间差
export function diffTime(date) {
  let dateTimeStamp = new Date(date).getTime()
  let minute = 1000 * 60
  let hour = minute * 60
  let day = hour * 24
  // let halfamonth = day * 15
  let month = day * 30
  let now = new Date().getTime()
  let diffValue = now - dateTimeStamp

  let monthC = diffValue / month
  // let weekC = diffValue / (7 * day)
  let dayC = diffValue / day
  let hourC = diffValue / hour
  let minC = diffValue / minute
  if (parseInt(monthC) >= 1) {
    return parseInt(monthC) + "个月前"
  } else if (parseInt(dayC) > 1) {
    return parseInt(dayC) + "天前"
  } else if (parseInt(dayC) === 1) {
    return "昨天"
  } else if (parseInt(hourC) >= 1) {
    return parseInt(hourC) + "小时前"
  } else if (parseInt(minC) >= 1) {
    return parseInt(minC) + "分钟前"
  } else {
    return "刚刚"
  }
}
